﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Measures;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Analytics.DataObjects.Visuals;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.Nodes
{
    /// <summary>
    /// Interaction logic for ClusterNode.xaml
    /// </summary>
    public partial class ClusterNode : EntityControlBase
    {
        private NetworkCluster _Cluster;
        /// <summary></summary>
        public NetworkCluster Cluster
        {
            get
            {
                return this._Cluster;
            }
            set
            {
                this._Cluster = value;
            }
        }

        private EntityBase _Entity;
        private Network _ParentNetwork;
        private VerticeMeasure _Measure;
        private VerticeMeasureRange _Range;
        private VerticeVisual _Visual;
        private bool _Selected;

        public ClusterNode(EntityBase entity,
            Network parentNetwork,
            VerticeVisual vVisual, bool selected,
            VerticeMeasure vMeasure, 
            VerticeMeasureRange range)
        {
            InitializeComponent();
            this._Entity = entity;
            this._ParentNetwork = parentNetwork;
            this._Measure = vMeasure;
            this._Range = range;
            this._Visual = vVisual;
            this._Selected = selected;

            if(this._Entity is NetworkCluster)
            {
                this.RenderCluster(this._ParentNetwork, this._Entity as NetworkCluster);
            }
        }

        private void RenderCluster(Network parentNetwork, NetworkCluster cluster)
        {
            Dictionary<EntityType,List<string>> clusterEntities =new Dictionary<EntityType, List<string>>();
            cluster.GetAllNestedEntityKeys(ref clusterEntities);
            string toolTip = string.Empty;
            int totalEntityCount = 0;
            foreach(EntityType et in clusterEntities.Keys)
            {
                if (toolTip.Length > 0)
                    toolTip += ", ";
                toolTip += et.ToString() + ": " + clusterEntities[et].Count.ToString();
                totalEntityCount += clusterEntities[et].Count;
            }
            this.txtTooltip.Text = toolTip;
            this.txtClusterSummary.Text = "Entities: " + totalEntityCount.ToString();
        }

        #region Overrides of EntityControlBase

        public override void SwitchableControl(BitmapImage imgSrc, bool selected)
        {
        }

        public override void SetDisabled(bool disabled)
        {
            this.SetValue(OpacityProperty, 0.1);
        }

        public override void SetHighlighted(bool highlighted)
        {
            if (highlighted)
                this.State = VerticeState.Highlighted;
            else
                this.State = VerticeState.Normal;

            
        }

        public override Size ImageSize
        {
            get { return new Size(this.ActualWidth, this.ActualHeight); }
        }

        public override void UpdateVisual()
        {
            
        }

        #endregion

        private void btnExpandCluster_Click(object sender, RoutedEventArgs e)
        {
            this.CollapsedCluster.Visibility = Visibility.Collapsed;
            this.ClusterDiagram.Visibility = Visibility.Visible;
            this.btnCloseCluster.Visibility = System.Windows.Visibility.Visible;
        }

        void mnuCollapse_Click(object sender, RoutedEventArgs e)
        {
            this.CollapsedCluster.Visibility = Visibility.Visible;
            this.ClusterDiagram.Visibility = Visibility.Collapsed;
            this.btnCloseCluster.Visibility = System.Windows.Visibility.Collapsed;
        }

        #region network
        public Network GetClusterSubNet(Network parentNetwork, NetworkCluster cluster)
        {
            Dictionary<EntityType, List<string>> entityKeys = new Dictionary<EntityType, List<string>>();
            cluster.GetAllNestedEntityKeys(ref entityKeys);
            Dictionary<EntityType, Dictionary<string, EntityBase>> allEntities =
                parentNetwork.GetEntities();
            List<EntityBase> clusterEntities=new List<EntityBase>();
            foreach(EntityType et in entityKeys.Keys)
            {
                List<string> keys = entityKeys[et];
                foreach(string key in keys)
                {
                    if(allEntities.ContainsKey(et) && 
                        allEntities[et].ContainsKey(key))
                    {
                        clusterEntities.Add(allEntities[et][key]);
                    }
                }
            }
            List<Link> links =
                Network.GetLinksConnectedToEntities(
                    parentNetwork.Links, clusterEntities);

            Network subNet = new Network(
                cluster.ClusterName, clusterEntities, links);

            return subNet;
        }
        #endregion

        private void btnCloseCluster_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
